package com.seaengine.util
{
	import flash.utils.Dictionary;


	public class ChineseUtil
	{
		public static var numbers:Dictionary;
		public static var units:Dictionary;

		public static function getNumbers():Dictionary
		{
			if (!numbers)
			{
				var dic:Dictionary = new Dictionary();

				dic[0] = '零';
				dic[1] = '一';
				dic[2] = '二';
				dic[3] = '三';
				dic[4] = '四';
				dic[5] = '五';
				dic[6] = '六';
				dic[7] = '七';
				dic[8] = '八';
				dic[9] = '九';
				numbers = dic;

			}

			return numbers;
		}

		public static function getUnits():Dictionary
		{
			if (!units)
			{
				var dic:Dictionary = new Dictionary();

				dic[1] = '';
				dic[10] = '十';
				dic[100] = '百';
				dic[1000] = '千';
				dic[10000] = '万';
				dic[100000000] = '亿';
				/**一兆 究竟等于 一百万，还是 一万亿，还是 一亿亿？*/

				units = dic;
			}

			return units;
		}

		public static function getChinese(num:Number):String
		{
			var rslt:String = '';
			var nZero:int = 0;
			var bZero:Boolean = false; /**是否应该有'零'：当低位有不为零时*/
			var unit:int = 10;
			var numberAndUnit:String = '';
			while (num)
			{
				var remainder:int = num % unit;
				if (remainder != 0)
				{
					bZero = true;

					if (unit > 1000)
					{
						unit /= 1000;
						bZero = false;
					}

					if (int(num) == 1 && unit / 10 == 10)
						numberAndUnit = getUnits()[unit / 10]; /**'十'*/
					else
						numberAndUnit = getNumbers()[remainder] + getUnits()[unit / 10];

					if (nZero)
						rslt = numberAndUnit + '零' + rslt;
					else
					{
						rslt = numberAndUnit + rslt;
						nZero = 0;
					}
				}
				else if (bZero)
					nZero++;

				unit *= 10;
				num /= 10;
			}

			return rslt;
		}

		/**
		 * 金额：数字转换成中文
		 * @param numberValue
		 * @return
		 *
		 */
		public static function atoc(_numberValue:int):String
		{
			var numberValue:String = new String(Math.round(_numberValue * 100)); // 数字金额  
			var chineseValue:String = ""; // 转换后的汉字金额  
			var String1:String = "零一二三四五六七八九"; // 汉字数字  
//			var String1:String = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字  
			var String2:String = "万千百十亿千百十万千百十元角分"; // 对应单位
//			var String2:String = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位    
			var len:int = numberValue.length; // numberValue 的字符串长度  
			var Ch1:String; // 数字的汉语读法  
			var Ch2:String; // 数字位的汉字读法  
			var nZero:int = 0; // 用来计算连续的零值的个数  
			var String3:int; // 指定位置的数值  
			if (len > 15)
			{
				//alert("超出计算范围");
				return "超出计算范围";
			}
			if (_numberValue == 0)
			{
				chineseValue = "零";
//				chineseValue = "零元整";
				return chineseValue;
			}

			String2 = String2.substr(String2.length - len, len); // 取出对应位数的STRING2的值  

			for (var i:int = 0; i < len; i++)
			{
				String3 = parseInt(numberValue.substr(i, 1), 10); // 取出需转换的某一位的值  
				//alert(String3);  
				if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15))
				{
					if (String3 == 0)
					{
						Ch1 = "";
						Ch2 = "";
						nZero = nZero + 1;
					}
					else if (String3 != 0 && nZero != 0)
					{
						Ch1 = "零" + String1.substr(String3, 1);
						Ch2 = String2.substr(i, 1);
						nZero = 0;
					}
					else
					{
						Ch1 = String1.substr(String3, 1);
						Ch2 = String2.substr(i, 1);
						nZero = 0;
					}
				}
				else
				{ // 该位是万亿，亿，万，元位等关键位  
					if (String3 != 0 && nZero != 0)
					{
						Ch1 = "零" + String1.substr(String3, 1);
						Ch2 = String2.substr(i, 1);
						nZero = 0;
					}
					else if (String3 != 0 && nZero == 0)
					{
						Ch1 = String1.substr(String3, 1);
						Ch2 = String2.substr(i, 1);
						nZero = 0;
					}
					else if (String3 == 0 && nZero >= 3)
					{
						Ch1 = "";
						Ch2 = "";
						nZero = nZero + 1;
					}
					else
					{
						Ch1 = "";
						Ch2 = String2.substr(i, 1);
						nZero = nZero + 1;
					}

					if (i == (len - 11) || i == (len - 3))
					{ // 如果该位是亿位或元位，则必须写上  
						Ch2 = String2.substr(i, 1);
					}

				}
				chineseValue = chineseValue + Ch1 + Ch2;

			}

			if (String3 == 0)
			{ // 最后一位（分）为0时，加上"整"  
				chineseValue = chineseValue + "";
//				chineseValue = chineseValue + "整";
			}

			return chineseValue;
		}
	}
}
